const { Controller } = require('egg');
const jwt = require('jsonwebtoken');
let { md5 } = require('crypto.js')

class HomeController extends Controller {
  async zc() {
    const { ctx } = this;
    const {userName,phone,passWord} = ctx.request.body
    const result = await this.app.mysql.insert('user', { 
        userName,
        phone,
        passWord
     }); 
    if(!result){
        ctx.body = {
            message:'注册失败',
            code:0
        };
        if(result.affectedRows===0){
            ctx.body = {
            message:'注册成功',
            code:1
        };
        }
    }
    else{
        ctx.body = {
            message:'没有该用户，请先注册',
            code:0
        }
    }
}
  async dl() {
    const { ctx } = this;
    const {userName,phone,passWord} = ctx.request.body
    var token = jwt.sign({ passWord }, 'user');
    const result = await this.app.mysql.get('user', { 
        userName,
        phone,
        passWord
     }); 
    if(md5(passWord)==passWord){
        ctx.body = {
            message:'登录成功',
            token,
            code:1
        };
        if(result.affectedRows===0){
            ctx.body = {
            message:'登录失败',
            code:1
        };
        }
    }
    else{
        ctx.body = {
            message:'没有该用户，请先注册',
            code:0
        }
    }
    localStorage.setItem('token')
  }
}

module.exports = HomeController;